//
//  NSDictionary+FilterNull.m
//  mmbang
//
//  Created by yaodaobin on 2016/12/7.
//  Copyright © 2016年 iyaya. All rights reserved.
//

#import "NSDictionary+FilterNull.h"

@implementation NSDictionary (FilterNull)

- (NSDictionary *)deleteAllNullValue {
    NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] init];

    for (NSString *keyStr in self.allKeys) {
        id valueObject = [self objectForKey:keyStr];
        if ([valueObject isKindOfClass:[NSArray class]]) {
            NSArray *arr = [self parseArray:(NSArray *)valueObject];
            [mutableDic safeSetObject:arr forKey:keyStr];
        } else if ([valueObject isKindOfClass:[NSDictionary class]]){
            NSDictionary *value = [(NSDictionary *)valueObject deleteAllNullValue];
            [mutableDic safeSetObject:value forKey:keyStr];
        } else {
            if ([valueObject isEqual:[NSNull null]]) {
                [mutableDic setObject:@"" forKey:keyStr];
            } else {
                [mutableDic setObject:valueObject forKey:keyStr];
            }
        }
    }
    return mutableDic;
}

- (NSArray *)parseArray:(NSArray *)array {
    NSMutableArray *tempArr = [NSMutableArray array];
    for (NSObject *object in array) {
        if ([object isKindOfClass:[NSDictionary class]]) {
            [tempArr addObject: [(NSDictionary *)object deleteAllNullValue]];
        } else if ([object isKindOfClass:[NSArray class]]) {
            [tempArr addObject : [self parseArray:(NSArray *)object]];
        }
    }
    return tempArr;
}

@end
